package com.erpanderp.wam.tm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.tm.model.entity.RRFBudget;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateBudgetBean implements Serializable{

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();
	
	private RRFBudget rrfBudget=new RRFBudget();
	
	private Map<String, String> modulesMap = new HashMap<String, String>();
	private List<SAPERPModule> modulesList = new ArrayList<SAPERPModule>();
	
	private SAPERPModuleDAO saperpModuleDAO = new SAPERPModuleDAO();
	
	private boolean FORM_VALID;

	public CreateBudgetBean() {
		
		
		modulesList = saperpModuleDAO.getSAPERPModules();
		for (SAPERPModule saperpModule : modulesList) {
			modulesMap.put(saperpModule.getDisplayName(),saperpModule.getDisplayName());			
		}
	}
	
	public void getTotalBudgetListener(AjaxBehaviorEvent e){
		
				
		/*if(rrfBudget.getQ1Budget()== null || rrfBudget.getQ1Budget()== 0){
			sapExperience = 0.0f;
		}
		if(rrfBudget.getQ2Budget() == null || rrfBudget.getQ2Budget() == 0.0){
			endUserExperience = 0.0f;
		}
		if(rrfBudget.getQ3Budget() == null || rrfBudget.getQ3Budget()== 0.0){
			nonSAPExperience = 0.0f;
		}
		if(rrfBudget.getQ4Budget() == null || rrfBudget.getQ4Budget()== 0.0){
			nonSAPExperience = 0.0f;
		}
		*/
		
		int totalBudget=rrfBudget.getQ1Budget()+rrfBudget.getQ2Budget()+rrfBudget.getQ3Budget()+rrfBudget.getQ4Budget();
		
		System.out.println("TOTAL BUDGET ::::::::"+totalBudget);
		rrfBudget.setTotalBudget(totalBudget);
		
		
	}
	
	public void confirmationAction(ActionEvent e){
		validateForm();	
		if(FORM_VALID){	
			RequestContext.getCurrentInstance().execute("budgetConfirmation.show();");
		}else {
			RequestContext.getCurrentInstance().execute("budgetConfirmation.hide();");
		}
    }

	
	//VALIDATIONS 
	private void validateForm(){
		FORM_VALID=true;		
		if(ValidationUtil.isEmpty(rrfBudget.getModuleName(), "Module Name", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
		}
		if(Integer.toString(rrfBudget.getFinancialYear()) != null){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Financial Year cannot be empty", ""));
		}
		if(Integer.toString(rrfBudget.getQ1Budget()) != null){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Q1 Budget cannot be empty", ""));
		}
		if(Integer.toString(rrfBudget.getQ2Budget()) != null){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Q2 Budget cannot be empty", ""));
		}
		if(Integer.toString(rrfBudget.getQ3Budget()) != null){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Q3 Budget cannot be empty", ""));
		}
		if(Integer.toString(rrfBudget.getQ4Budget()) != null){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Q4 Budget cannot be empty", ""));
		}
		
		if(rrfBudget.getQ1Budget() < 0){
			 FORM_VALID = false;
			 FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Q4 Budget cannot be empty", ""));
		}
	}
	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public RRFBudget getRrfBudget() {
		return rrfBudget;
	}

	public void setRrfBudget(RRFBudget rrfBudget) {
		this.rrfBudget = rrfBudget;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public SAPERPModuleDAO getSaperpModuleDAO() {
		return saperpModuleDAO;
	}

	public void setSaperpModuleDAO(SAPERPModuleDAO saperpModuleDAO) {
		this.saperpModuleDAO = saperpModuleDAO;
	}

	public List<SAPERPModule> getModulesList() {
		return modulesList;
	}

	public void setModulesList(List<SAPERPModule> modulesList) {
		this.modulesList = modulesList;
	}

}
